HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) {
   var r = this.ownerDocument.createRange();
   r.setStartBefore(this);
   var df = r.createContextualFragment(sHTML);
   this.parentNode.replaceChild(df, this);
});

function quickWatchArtist(artist_id)
{
    var gt = new Gettext({ domain : 'musdb', locale_data : json_locale_data});

    var rr = document.getElementById('quick_watch');
    var button = document.getElementById('qwatch_button');
    if(rr)
        button.disabled=true;

    $.ajax({
            url: '/?cat=add_watch',
            data: 'action=watch_artist&artist_id=' + artist_id,
            dataType: 'json',
            type: 'post',
            success: function (j) {
                    if(j.ok)
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;"><input id="qunwatch_button" type="button" value="'+gt.gettext("Unwatch artist")+'" onclick="quickUnwatchArtist(\''+artist_id+'\'); return false;"></div>';
                        }
                    else
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;">'+gt.gettext("Error")+'</div>';
                        }
                }
            });
}

function quickUnwatchArtist(artist_id)
{
    var gt = new Gettext({ domain : 'musdb', locale_data : json_locale_data});

    var rr = document.getElementById('quick_watch');
    var button = document.getElementById('qunwatch_button');
    if(rr)
        button.disabled=true;

    $.ajax({
            url: '/?cat=add_watch',
            data: 'action=unwatch_artist&artist_id=' + artist_id,
            dataType: 'json',
            type: 'post',
            success: function (j) {
                    if(j.ok)
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;"><input id="qwatch_button" type="button" value="'+gt.gettext("Watch artist")+'" onclick="quickWatchArtist(\''+artist_id+'\'); return false;"></div>';
                        }
                    else
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;">'+gt.gettext("Error")+'</div>';
                        }
                }
            });
}

function quickWatchRelease(release_id)
{
    var gt = new Gettext({ domain : 'musdb', locale_data : json_locale_data});

    var rr = document.getElementById('quick_watch');
    var button = document.getElementById('qwatch_button');
    if(rr)
        button.disabled=true;

    $.ajax({
            url: '/?cat=add_watch',
            data: 'action=watch_release&release_id=' + release_id,
            dataType: 'json',
            type: 'post',
            success: function (j) {
                    if(j.ok)
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;"><input id="qunwatch_button" type="button" value="'+gt.gettext("Unwatch release")+'" onclick="quickUnwatchRelease(\''+release_id+'\'); return false;"></div>';
                        }
                    else
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;">'+gt.gettext("Error")+'</div>';
                        }
                }
            });
}

function quickUnwatchRelease(release_id)
{
    var gt = new Gettext({ domain : 'musdb', locale_data : json_locale_data});

    var rr = document.getElementById('quick_watch');
    var button = document.getElementById('qunwatch_button');
    if(rr)
        button.disabled=true;

    $.ajax({
            url: '/?cat=add_watch',
            data: 'action=unwatch_release&release_id=' + release_id,
            dataType: 'json',
            type: 'post',
            success: function (j) {
                    if(j.ok)
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;"><input id="qwatch_button" type="button" value="'+gt.gettext("Watch release")+'" onclick="quickWatchRelease(\''+release_id+'\'); return false;"></div>';
                        }
                    else
                        {
                            var r = document.getElementById('quick_watch');
                            if(r)
                                r.outerHTML = '<div id="quick_watch" style="float: left;">'+gt.gettext("Error")+'</div>';
                        }
                }
            });
}


function watchRelease(release_id)
{
    var gt = new Gettext({ domain : 'musdb', locale_data : json_locale_data});

    var rr = document.getElementById('w' + release_id + 'r');
    if(rr)
        rr.outerHTML = '<span id="w' + release_id + 'r">'+gt.gettext("Please wait...")+'</span>';
    
    var ru = document.getElementById('w' + release_id + 'u');
    if(ru)
        ru.outerHTML = '<span id="w' + release_id + 'u">'+gt.gettext("Please wait...")+'</span>';

    $.ajax({
            url: '/?cat=add_watch',
            data: 'action=watch_release&release_id=' + release_id,
            dataType: 'json',
            type: 'post',
            success: function (j) {
                    if(j.ok)
                        {
                            var r = document.getElementById('w' + release_id + 'r');
                            if(r)
                                r.outerHTML = gt.gettext("Watching");
                            var u = document.getElementById('w' + release_id + 'u');
                            if(u)
                                u.outerHTML = gt.gettext("Watching");
                        }
                    else
                        {
                            var r = document.getElementById('w' + release_id + 'r');
                            if(r)
                                r.outerHTML = gt.gettext("Error");
                            var u = document.getElementById('w' + release_id + 'u');
                            if(u)
                                u.outerHTML = gt.gettext("Error");
                        }
                }
            });
}
